/**
 *  Copyright (c) 2014 The xmlepf project.  All rights reserved.
 *
 *  This file is part of xmlepf (Event Driven Testing Framework).
 *
 *  xmlepf is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  xmlepf is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with xmlepf.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This software consists of voluntary contributions made by many
 *  individuals.  For exact contribution history, see the revision
 *  history and logs, available at http://code.google.com/p/xmlepf.
 */
package org.xmlepf.utils;

import java.util.List;
import org.xmlepf.xml.adapter.XPath;
import org.xmlepf.xml.adapter.XMLNode;
import org.xmlepf.xml.adapter.saxon.NodeSerializerImp;
import org.xmlepf.xml.adapter.saxon.XPathImp;

/**
 *
 *
 */
public class ConfigurationLoader {

    private Configuration configuration;
    private XPath xPath;
    private NodeSerializerImp s;

    public ConfigurationLoader(Configuration configuration) {
        this.configuration = configuration;
    }

    public synchronized Configuration initialize(String configXml) throws Exception {
        performInitialization(configXml);
        loadGroups();
        return configuration;
    }

    private void performInitialization(String configXml) throws Exception {
        xPath = new XPathImp();
        s = new NodeSerializerImp();
        xPath.initialize(configXml);
    }

    private void loadGroups() throws Exception {
        List<XMLNode> groupNodes = xPath.query("/config/child::*");
        for (XMLNode groupNode : groupNodes) {
            String group = groupNode.getLocalPart();
            loadProperties(group);
        }
    }

    private void loadProperties(String group) throws Exception {
        List<XMLNode> propertyNodes = xPath.query("//property");
        for (XMLNode node : propertyNodes) {
            String key = getKey(node);
            String value = getValue(node);
            configuration.setProperty(group + "." + key, value);
        }
    }

    private String getKey(XMLNode node) throws Exception {
        XPath xPathTmp = new XPathImp();
        xPathTmp.initialize(node);
        String key = s.serialize(xPathTmp.query("@key"));
        return key;
    }

    private String getValue(XMLNode node) throws Exception {
        XPath xPathTmp = new XPathImp();
        xPathTmp.initialize(node);
        String value = s.serialize(xPathTmp.query("child::*"));

        // todo: find a more consistent way of detecting that the child node is a text node
        if (value.isEmpty()) {
            value = node.getStringValue();
        }

        return value;
    }
}
